package com.yisu.auction.api.domain.utils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

/**
 * author: 赵杰
 * Business Description：IP地理位置常规方法
 * createTime：2017/8/11
 */
public class IpAddrUtil {
    /**
     * author: 赵杰
     * Business Description：获取客户端真实IP
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }


    public static String GetIp() {
        InetAddress ia = null;
        try {
            ia = ia.getLocalHost();

            String localname = ia.getHostName();
            String localip = ia.getHostAddress();
            System.out.println("本机名称是：" + localname);
            System.out.println("本机的ip是 ：" + localip);
        } catch (Exception e) {
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        return ia.getHostAddress();
    }

    /**
     * 获得客户端真实ip所属地
     */
    public static String getAddress(String IP) {
        String address = "";
        String filePath = IPSeeker.class.getClassLoader()
                .getResource("").getPath()
                + "ip.dat";
        IPSeeker seeker = IPSeeker.getInstance(filePath);
        address = seeker.getAddress(IP);
        return address;
    }

}
